#pragma once
//数据库版本
#include "../comm/util.hpp"
#include "../comm/log.hpp"

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <fstream>
#include <cstdlib>
#include <cassert>
#include "include/mysql.h"

// 根据题目list文件，加载所有的题目信息到内存中
// model: 主要用来和数据进行交互，对外提供访问数据的接口

namespace ns_model
{
    using namespace ns_util;
    using namespace ns_log;
    struct Question
    {
        std::string number; // 题目id
        std::string title; // 题目标题
        std::string star; // 题目星级
        int cpu_limit; // cpu限制
        int mem_limit; // 内存限制
        std::string desc; // 题目描述
        std::string header;//给用户预设的代码
        std::string tail;//测试用例
    };
    const std::string oj_questions = "ojquestions";
    const std::string host = "127.0.0.1";
    const std::string user = "oj_client";
    const std::string passwd = "123456";
    const std::string db = "oj";
    const int port = 3306;
    class Model
    {
    private:
        std::unordered_map<std::string, Question> questions; // 题目id和题目内容的映射
    public:
        Model()
        {
        }
        ~Model() {}
        bool QueryMysql(std::string& sql,std::vector<Question> *out)
        {
            MYSQL *my= mysql_init(nullptr);
            if (mysql_real_connect(my,host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0) == nullptr)
            {
                LOG(FATAL) << "连接数据库失败" << "\n";
                return false;
            }
            mysql_set_character_set(my,"utf8");
            LOG(INFO)<< "连接数据库成功" << "\n";
            if (mysql_query(my,sql.c_str())!=0)
            {
                LOG(ERROR) << "查询失败" << "\n";
                return false;
            }
            MYSQL_RES *res = mysql_store_result(my);
            int rows = mysql_num_rows(res);
            int cols = mysql_num_fields(res);
            Question q;
            for(int i=0;i<rows;i++)
            {
                MYSQL_ROW row = mysql_fetch_row(res);
                q.number=row[0];
                q.title=row[1];
                q.star=row[2];
                q.desc=row[3];
                q.header=row[4];
                q.tail=row[5];
                q.cpu_limit=atoi(row[6]);
                q.mem_limit=atoi(row[7]);
                out->push_back(q);
            }
            free(res);
            mysql_close(my);
            return true;
        }
        bool GetAllQuestions(std::vector<Question>* out)
        {
           std::string  sql="select * from "+oj_questions;
            return QueryMysql(sql,out);
        }
        bool GetOneQuestion(const std::string& number, Question* question)
        {
           bool res=false;
           std::string sql="select * from "+oj_questions+" where number="+number;
           std::vector<Question> questions;
            if (QueryMysql(sql,&questions))
            {
                if(questions.size() == 1)
                {
                    *question = questions[0];
                    res=true;
                }
            }
            return res;
        }


    };
}